package com.ruoyi.performance.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.performance.domain.PerfDitemScore;
import com.ruoyi.performance.mapper.PerfTemplateDitemMapper;
import com.ruoyi.performance.service.PerfDitemScoreService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author YL
 * @date 2020-07-22
 * @description 绩效细项打分控制层
 */
@RestController
@RequestMapping("score")
public class PerfDitemScoreController extends BaseController {
	
	@Resource
	private PerfDitemScoreService perfDitemScoreService;

	@Resource
	private PerfTemplateDitemMapper perfTemplateDitemMapper;

	/**
	 * 查询细项得分
	 *
	 * @param perfDitemScore 细项ID
	 * @return 细项得分
	 */
	@GetMapping("get")
	public R list(PerfDitemScore perfDitemScore){
		Map<String, Object> m = new HashMap<>(16);
		m.put("rows", perfDitemScoreService.selectPerfDitemScoreById(perfDitemScore));
		return R.ok(m);
	}

	/**
	 * 获取考核单中细项总分
	 *
	 * @param checkId 考核单ID
	 * @return 细项得分
	 */
	@GetMapping("all")
	public R all(int checkId){
		Map<String, Object> m = new HashMap<>(16);
		m.put("count", perfDitemScoreService.selectPerfDitemScoreByCheckId(checkId));
		return R.ok(m);
	}

	/**
	 * 新增细项得分
	 *
	 * @param perfDitemScore 细项得分
	 * @return 结果
	 */
	@PostMapping("save")
	public R addSave(@RequestBody PerfDitemScore perfDitemScore) {
		//获取细项ID
		int ditemId=perfDitemScore.getDitemId();
		//根据细项ID获取该细项名
		String ditemName  = perfTemplateDitemMapper.selectPerfTemplateDitemById(ditemId).getDitemName();
		//获取该细项名
		int	row=perfDitemScoreService.insertPerfDitemScore(perfDitemScore);
		if (row==-1){
			return R.error(505,ditemName+"分数超过范围！");
		}
		return toAjax(row);
	}

	/**
	 * 修改细项得分
	 *
	 * @param perfDitemScore 细项得分
	 * @return 结果
	 */
	@PostMapping("update")
	public R editSave(@RequestBody PerfDitemScore perfDitemScore) {
		return toAjax(perfDitemScoreService.updatePerfDitemScore(perfDitemScore));
	}


}
